package week2;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Created by dyc on 2016/11/17.
 */
public class LinkQueue<Item> extends AbstractQueue<Item> {
    private Node first;
    private Node last;
    private int N;

    @Override
    public void enqueue(Item item) {
        if (item == null)
            throw new NullPointerException();
        N++;
        Node lastNode = last;
        last = new Node();
        last.data = item;
        if (isEmpty())
            first = last;
        else
            lastNode.next = last;

    }

    @Override
    public Item dequeue() {
        if (first == null)
            throw new NoSuchElementException();
        N--;
        Item data = first.data;
        first = first.next;
        if (first == null)
            last = null;
        return data;
    }

    @Override
    public boolean isEmpty() {
        return first == null;
    }

    @Override
    public int size() {
        return N;
    }

    @Override
    public Iterator<Item> iterator() {
        return null;
    }

    private class QueueIterator implements Iterator<Item>{
        Node current = first;
        @Override
        public boolean hasNext() {
            return first != null;
        }

        @Override
        public Item next() {
            Item item = first.data;
            first = first.next;
            return item;
        }
    }

    private class Node{
        Item data;
        Node next;
    }
}
